#include "Maze.hpp"

// Namespaces this class lives in
namespace mindsweeper {
namespace maze {

Maze::Maze(main::GameMain *game_main) :
	gameMain(game_main)
{
	// Initialize time to zero
	this->time = 0.0;
}

void Maze::init() {
	// Set color and depth clear value
	glClearDepth(1.f);
	glClearColor(0.f, 0.f, 0.f, 0.f);
	
	// Enable Z-buffer read and write
	glEnable(GL_DEPTH_TEST);
	glDepthMask(GL_TRUE);
	
	// Setup a perspective projection
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(90.f, 1.f, 1.f, 500.f);
}

void Maze::processEvent(sf::Event &event) {
	switch (event.Type) {
		case sf::Event::Resized:
			// Resize our rendering viewport.
			glViewport(0, 0, event.Size.Width, event.Size.Height);
			break;
	}
}

void Maze::update() {
	this->time += this->gameMain->getTimeHandler().getDt();
}

void Maze::render() {
	// Clear out previous image
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	// Render a rotating cube
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	glTranslatef(0.f, 0.f, -200.f);
	glRotatef(this->time * 50, 1.f, 0.f, 0.f);
	glRotatef(this->time * 30, 0.f, 1.f, 0.f);
	glRotatef(this->time * 90, 0.f, 0.f, 1.f);
	glBegin(GL_QUADS);

	glVertex3f(-50.f, -50.f, -50.f);
	glVertex3f(-50.f,  50.f, -50.f);
	glVertex3f( 50.f,  50.f, -50.f);
	glVertex3f( 50.f, -50.f, -50.f);

	glVertex3f(-50.f, -50.f, 50.f);
	glVertex3f(-50.f,  50.f, 50.f);
	glVertex3f( 50.f,  50.f, 50.f);
	glVertex3f( 50.f, -50.f, 50.f);

	glVertex3f(-50.f, -50.f, -50.f);
	glVertex3f(-50.f,  50.f, -50.f);
	glVertex3f(-50.f,  50.f,  50.f);
	glVertex3f(-50.f, -50.f,  50.f);

	glVertex3f(50.f, -50.f, -50.f);
	glVertex3f(50.f,  50.f, -50.f);
	glVertex3f(50.f,  50.f,  50.f);
	glVertex3f(50.f, -50.f,  50.f);

	glVertex3f(-50.f, -50.f,  50.f);
	glVertex3f(-50.f, -50.f, -50.f);
	glVertex3f( 50.f, -50.f, -50.f);
	glVertex3f( 50.f, -50.f,  50.f);

	glVertex3f(-50.f, 50.f,  50.f);
	glVertex3f(-50.f, 50.f, -50.f);
	glVertex3f( 50.f, 50.f, -50.f);
	glVertex3f( 50.f, 50.f,  50.f);

	glEnd();
}

// End mainspaces
}
}
